package com.ge.energy.pgs.myfleet.code.user;

import java.io.IOException;

import com.ge.energy.pgs.myfleet.code.factory.BedoFunctionFactory;
import com.ge.energy.pgs.myfleet.code.factory.ChatRoomFactory;
import com.ge.energy.pgs.myfleet.code.factory.MyServletFactory;
import com.ge.energy.pgs.myfleet.code.vo.Contact;
import com.ge.energy.pgs.myfleet.code.vo.ContactCategory;
import com.ge.energy.pgs.myfleet.code.vo.Contacts;
import com.ge.energy.pgs.myfleet.code.wsvo.MyBedoData;
import com.ge.energy.pgs.myfleet.database.data.DataInitBean;
import com.ge.energy.pgs.myfleet.database.query.base.Select;
import com.ge.energy.pgs.myfleet.database.table.UserTable;

public class BedoFinalImpl extends BedoMenuIpml{
	public void $service_login(String idpwd){
		this.myInfo = null;
	
		String id = "";
		String password = "";
		if(idpwd != null && idpwd.length()>5){
			String[] str = idpwd.split("&");
			if(str.length == 2){
				id = str[0];
				password = str[1];
				
				try {
					if(DataInitBean.id$password.get(id).equals(password)){
						for(UserTable user : DataInitBean.userTable){
							if(user.getUserId().equals(id)){
								this.myInfo = new Contact();
								this.myInfo.setContactDec(user.getUserDec());
								this.myInfo.setContactId(user.getUserId());
								this.myInfo.setContactImg(user.getUserImg());
								this.myInfo.setContactName(user.getUserName());
								this.myInfo.setContactSex(user.getUserSex());
								break;
							}
						}
						if(this.myInfo != null){
							this.password = password;
							BedoFunctionFactory.bedofunctions.put(this.myInfo.getContactId(), this);
							
							MyBedoData bedoData = new MyBedoData();
							
							myContacts = Select.getContactList(id);
							checkOnlineStatus(myContacts,true);
							bedoData.setContacts(myContacts);
							bedoData.setChatRooms(Select.getRoomList());
							String bedoDataString = gson.toJson(bedoData);

							MyServletFactory.getSockets().get(this.myInfo.getContactId()).getConn().sendMessage("{\"flag\":\"$client_login\",\"status\":\"success\",\"backvalue\":"+ bedoDataString +"}");
						}else{
							MyServletFactory.getSockets().get(this.myInfo.getContactId()).getConn().sendMessage("{\"flag\":\"$client_login\",\"status\":\"不存在该用户！\",\"backvalue\":\"\"}");
						}
					}else{
						MyServletFactory.getSockets().get(this.myInfo.getContactId()).getConn().sendMessage("{\"flag\":\"$client_login\",\"status\":\"密码错误！\",\"backvalue\":\"\"}");
					}
				
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		

	}
	
	private void checkOnlineStatus(Contacts contacts,boolean status){
		for(ContactCategory categoty : contacts.getCategoryList()){
			for(Contact contact : categoty.getContactList()){
				if(BedoFunctionFactory.bedofunctions.containsKey(contact.getContactId())){
					
					String message = "";
					if(status){
						message = "online";
						contact.setOnline(true);
					}else{
						message = "offline";
					}
					
					String backMessage = "{\"flag\":\"$client_online_check\",\"status\":\""+this.myInfo.getContactId()+"\",\"backvalue\":\""+message+"\"}";
					
					
					try {
						MyServletFactory.getSockets().get(contact.getContactId()).getConn().sendMessage(backMessage);
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
			}
		}
	}
	
	public void close(){
		if(this.myInfo != null){
				$service_leaveRoom(roomId);
				checkOnlineStatus(myContacts,false);
				
				if(this.roomId != null){
					if(ChatRoomFactory.getChatRooms().containsKey(this.roomId )){
						ChatRoomFactory.getChatRooms().get(this.roomId).getChatRoomContacts().remove(this.myInfo.getContactId());
					}
				}
				
				if(MyServletFactory.getSockets().containsKey(this.myInfo.getContactId())){
					MyServletFactory.getSockets().remove(this.myInfo.getContactId());
				}
				this.myInfo = null;
		}
		
	}
	
	
	
}
